package cn.edu.dlut.ssdut.foodCredit2012.service;

/*import java.util.List;

import org.hibernate.Query;*/
import org.hibernate.Session;

import cn.edu.dlut.ssdut.foodCredit2012.model.Export;
import cn.edu.dlut.ssdut.foodCredit2012.model.ExportCollection;
import cn.edu.dlut.ssdut.foodCredit2012.util.HibernateUtil;

public class ExportService {
	
	public void add(Export ex) throws Exception {
		if(!hasExport(ex)){
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(ex);
			session.getTransaction().commit();
		}
	}
	
	public void delete(Export ex) throws Exception {
		if(hasExport(ex)){
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.delete(ex);
			session.getTransaction().commit();
		}
	}
	
	public void update (Export ex)throws Exception{
		if(hasExport(ex)){
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.update(ex);
			session.getTransaction().commit();
		}
	}
	
	/*public List exportList() {
		List exportList = null;
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("from Export");
		query.setFirstResult(2);
		query.setMaxResults(10);
		exportList = query.list();
		
		session.getTransaction().commit();		
		return exportList;
	}*/
	
	public boolean hasExport(Export ex) throws Exception {
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		long count = (Long)session.createQuery("select count(*) from Export ex where ex.licenceID = :licenceID")
			.setString("licenceID", ex.getLicenceID())
			.uniqueResult();
		session.getTransaction().commit();
		
		if(count > 0) return true;
		return false;
		
	}
	
	public Export load(Export ex) throws Exception{
		if(hasExport(ex)){
			ex = loadByLicenceID(ex.getLicenceID());
			return ex;
		}
		return ex;
	}
	
	public Export loadByLicenceID(String licenceID) throws Exception{
		Export ex = new Export();
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		ex = (Export)session.createQuery("select ex from Export ex where ex.licenceID = :licenceID")
			.setParameter("licenceID", licenceID)
			.uniqueResult();
		session.getTransaction().commit();
		return ex;
	}
	
	public Export exportCollectionToExport(ExportCollection exportC)throws Exception{
		Export ex = new Export();
		ex.setAnnualSurveyDate(exportC.getAnnualSurveyDate());
		ex.setAunnualSurveyResult(exportC.getAunnualSurveyResult());
		ex.setAuthorizedAgency(exportC.getAuthorizedAgency());
		ex.setExportID(exportC.getExportID());
		ex.setExportSign(exportC.getExportSign());
		ex.setLicenceID(exportC.getLicenceID());
		ex.setReceiveDate(exportC.getReceiveDate());
		
		return ex;
	}

}
